package com.sm.honkon.ui.model

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import au.com.safie.user.app.util.CacheUtil
import com.blankj.utilcode.util.ToastUtils
import com.sm.honkon.app.App
import com.sm.honkon.data.bean.CustomerEntity
import com.sm.honkon.data.bean.HandlerEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.livedata.BooleanLiveData

class HandleClientsInfoViewModel : BaseViewModel() {
    var curHandlerEntity : HandlerEntity? = null
    val dao = App.database.customerDao()
    val uid = CacheUtil.getCurUid()
    var user = MutableLiveData<CustomerEntity>(CustomerEntity(uid))
    val isMan = BooleanLiveData(true)
    val isEdit = BooleanObservableField(false)
    val bloods = arrayListOf("A", "B", "AB", "O", "其他")
    val shouzhen = arrayListOf("黄褐斑", "太田痣", "痤疮", "纹身", "雀斑", "老年斑", "嫩肤")


    fun insert(callback: () -> Unit) {
        user.value?.let {
            if (it.name.isEmpty()) {
                ToastUtils.showShort("请输入姓名")
                return@let
            }
            if (it.birthday.isEmpty()) {
                ToastUtils.showShort("请选择生日")
                return@let
            }
            if (it.blood.isEmpty()) {
                ToastUtils.showShort("请选择血型")
                return@let
            }
            if (it.mobile.isEmpty()) {
                ToastUtils.showShort("请输入本人电话")
                return@let
            }
            if (it.emergencyCall1.isEmpty()) {
                ToastUtils.showShort("请输入紧急电话")
                return@let
            }
            if (it.address.isEmpty()) {
                ToastUtils.showShort("请输入住址")
                return@let
            }
            if (it.firstTime.isEmpty()) {
                ToastUtils.showShort("请输入首诊时间")
                return@let
            }
            if (it.historyOfPastIllness.isEmpty()) {
                ToastUtils.showShort("请输入既往史")
                return@let
            }
            if (it.allergicHistory.isEmpty()) {
                ToastUtils.showShort("请输入过敏史")
                return@let
            }
            if (it.chiefComplaint.isEmpty()) {
                ToastUtils.showShort("请输入主诉")
                return@let
            }
            if (it.first_treatment_conetnt.isEmpty()) {
                ToastUtils.showShort("请输入首诊内容")
                return@let
            }
            viewModelScope.launch(Dispatchers.Default) {
                curHandlerEntity?.let { handlerEntity ->
                    it.handleId = handlerEntity.uid
                    it.handleName = handlerEntity.name
                }
                if (isEdit.get().not()) {
                    dao.insert(it)
                    CacheUtil.setLastUid(uid + 1)
                } else {
                    dao.update(it)
                }
                withContext(Dispatchers.Main) {
                    callback()
                }
            }
        }
    }

    fun setFirstTime() {
        user.value = user.value?.setFirstTime()
    }
}